AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include( "shared.lua" )
 
ENT.RenderGroup = RENDERGROUP_BOTH
ENT.Parts = { "a", "b", "c" }

function ENT:Initialize()

	self:SetModel( "models/buggy.mdl" )
	//self:PhysicsInitSphere( 7 )
	self:PhysicsInitBox( Vector( -8,-15,-10 ), Vector( 8,10,15 ) )
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid( SOLID_VPHYSICS )
	
	self:SetNWBool( "is_melon", true )
	
	local phys = self:GetPhysicsObject()
	if( phys:IsValid() ) then
		phys:Wake()
		phys:SetDamping( 9 )
		phys:SetMaterial( "ice" )
	end
	
	self:StartMotionController()
	
end

/* Keep Upright */
function ENT:ComputeRotSpeedToAlignAxes( testAxis, alignAxis, currentSpeed, damping, scale, maxSpeed )

	rotationAxis = testAxis:Cross( alignAxis )
	
	cosine = testAxis:DotProduct( alignAxis )
	sine = rotationAxis:Length()
	angle = math.atan2( sine, cosine )
	math.deg( angle )
	
	angular = rotationAxis * scale * angle
	angular = angular - rotationAxis * damping * currentSpeed:DotProduct( rotationAxis )
	
	len = angular:Length()
	
	if ( len > maxSpeed ) then
		len = maxSpeed;
	end
	
	return angular * len

end

function ENT:PhysicsSimulate( phys, delta )
	local ForceAngle, ForceLinear = self.ForceAngle, self.ForceLinear
	return ForceAngle, ForceLinear, SIM_LOCAL_ACCELERATION
end

function ENT:UpdateTransmitState()
	return TRANSMIT_ALWAYS
end

function ENT:UpdateMovement()
	local ply = self:GetOwner()
	if not ValidEntity( ply ) then self:Break() return end
	if ( SERVER and self:WaterLevel() > 0 ) then
		self:GetOwner():Kill()
	end
	local phys = self:GetPhysicsObject()
	
	-- Keep upright!
	if phys:IsValid() and not( self:GetAngles().p < 50 and self:GetAngles().p > -50 and self:GetAngles().r < 50 and self:GetAngles().r > -50 ) then
		ForceAngle = self:ComputeRotSpeedToAlignAxes( Vector(0,0,1), phys:LocalToWorldVector( Vector(0,0,1) ), phys:GetAngleVelocity(), 0, 1/FrameTime(), 4 )
		phys:AddAngleVelocity( -1*phys:GetAngleVelocity() + ForceAngle )
		if self:IsAboveGround( 15 ) then
			phys:AddVelocity( phys:GetVelocity() + Vector(0,0,300) ) -- Lift ourselves up in the air to aid keep upright.
		end
	end
	
	phys:SetDamping( 0, 0 )
	
	-- General movement.
	local direction = Vector(0,0,0)
	if( ply:KeyDown( IN_FORWARD ) ) then
		if self:GetOwner():KeyDown( IN_MOVELEFT ) then
			phys:AddAngleVelocity( Vector( 0, 0, self:GetVelocity():Length()*0.2 ) )
		elseif self:GetOwner():KeyDown( IN_MOVERIGHT ) then
			phys:AddAngleVelocity( Vector( 0, 0, -self:GetVelocity():Length()*0.2 ) )
		end
		direction = direction + self:GetAngles():Forward()
	end
	
	if( ply:KeyDown( IN_BACK ) ) then
		if self:GetOwner():KeyDown( IN_MOVELEFT ) then
			phys:AddAngleVelocity( Vector( 0, 0, -self:GetVelocity():Length()*0.2 ) )
		elseif self:GetOwner():KeyDown( IN_MOVERIGHT ) then
			phys:AddAngleVelocity( Vector( 0, 0, self:GetVelocity():Length()*0.2 ) )
		end
		direction = direction + self:GetAngles():Forward() * -1
	end
	
	if direction == Vector(0,0,0) then
		if self:GetOwner():KeyDown( IN_MOVELEFT ) then
			phys:AddAngleVelocity( Vector( 0, 0, self:GetVelocity():Length()*0.2 ) )
		elseif self:GetOwner():KeyDown( IN_MOVERIGHT ) then
			phys:AddAngleVelocity( Vector( 0, 0, -self:GetVelocity():Length()*0.2 ) )
		end
	end
	
	/*if self:GetOwner():KeyDown( IN_MOVELEFT ) then
		phys:AddAngleVelocity( Vector( 0, 0, -self:GetVelocity():Length()*100 ) )
	elseif self:GetOwner():KeyDown( IN_MOVERIGHT ) then
		phys:AddAngleVelocity( Vector( 0, 0, self:GetVelocity():Length()*100 ) )
	end*/
	
	local velocity = direction * GAMEMODE.SpeedMultiplier
	if( velocity:LengthSqr() > 0 and phys:IsValid() ) then
		phys:SetDamping( 2, 5 )
	elseif( phys:IsValid() and self:IsAboveGround( 15 ) ) then
		phys:SetDamping( 9, 5 )
	elseif( phys:IsValid() ) then
		phys:SetDamping( 0, 5 )
	end
	
	if( velocity:LengthSqr() > 0 and ( self:IsAboveGround( 15 ) or self:WaterLevel() > 0 ) ) then
		-- We don't want melons to fly.
		velocity.z = velocity.z * 0
		GAMEMODE:ApplyMelonVelocity( self, LerpVector( GAMEMODE.SpeedHandling, self:GetVelocity(), velocity ) )
	end
end

function ENT:Break()

	for i = 1, 3 do
	
		local gib = ents.Create( "prop_physics_multiplayer" )
		gib:SetModel( "models/props_junk/watermelon01_chunk01"..self.Parts[i]..".mdl" )
		gib:SetPos( self:GetPos() )
		gib:Spawn()
		gib:Activate()
		
		gib:GetPhysicsObject():ApplyForceCenter( self:GetVelocity() * 5 )
		
		timer.Simple( math.Rand( 5, 10 ), function() if( ValidEntity( gib ) ) then gib:Remove() end end )
	end
	
	-- Remove the melon
	self:Remove()
	
end